<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <!-- 引入组件库 -->
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script src="../js/vue.js"></script>
    <script src="../plugins/elementui/index.js"></script>
    <script src="../js/axios-0.18.0.js"></script>
    <style>
        .datatable {
            position: relative;
            box-sizing: border-box;
            -webkit-box-flex: 1;
            width: 100%;
            max-width: 100%;
            font-size: 14px;
            color: rgb(96, 98, 102);
            overflow: hidden;
            flex: 1 1 0%;
        }

        .datatable td, .datatable th {
            padding: 12px 0;
            min-width: 0;
            -webkit-box-sizing: border-box;
            box-sizing: border-box;
            text-overflow: ellipsis;
            vertical-align: middle;
            position: relative;
            text-align: left;
        }
    </style>
</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>用户信息</h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>用户信息</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <el-input placeholder="请输入姓名" v-model="pagination.queryString" style="width: 200px;"
                          class="filter-item"
                          @keyup.enter.native="handleFilter"></el-input>
                <el-button @click="handleCurrentChange(1)" class="dalfBut">查询</el-button>
                <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
            </div>
            <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                <el-table-column type="index" align="center" label="序号"></el-table-column>
                <el-table-column prop="username" label="姓名" align="center"></el-table-column>
                <el-table-column prop="birthday" label="出生日期" align="center"></el-table-column>
                <el-table-column prop="gender" label="性别" align="center"></el-table-column>
                <el-table-column prop="telephone" label="联系方式" align="center"></el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                        <!--<el-button type="primary" size="mini" @click="handleUpdatePassword(scope.row)">修改密码</el-button>-->
                        <el-button type="primary" size="mini" @click="handleResetPassword(scope.row)">重置密码
                        </el-button>
                        <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>
            <!-- 新增标签弹层 -->
            <div class="add-form">
                <el-dialog title="新增用户" :visible.sync="dialogFormVisible">
                    <template>
                        <el-tabs v-model="activeName" type="card">
                            <el-tab-pane label="基本信息" name="first">
                                <el-form label-position="right" label-width="50px">
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="姓名">
                                                <el-input v-model="formData.username"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="出生日期">
                                                <el-input placeholder="YYYY-MM-DD" v-model="formData.date"
                                                          type="textarea"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="性别">
                                                <el-select v-model="formData.gender">
                                                    <el-option label="男" value="男"></el-option>
                                                    <el-option label="女" value="女"></el-option>
                                                </el-select>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="联系方式">
                                                <el-input v-model="formData.telephone" type="textarea"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                </el-form>
                            </el-tab-pane>
                            <el-tab-pane label="角色信息" name="second">
                                <div class="checkScrol">
                                    <table class="datatable">
                                        <thead>
                                        <tr>
                                            <th>选择</th>
                                            <th>角色名</th>
                                        </tr>
                                        </thead>
                                        <tbody>
                                        <tr v-for="c in tableData">
                                            <td>
                                                <input :id="c.id" v-model="roleIds" type="checkbox" :value="c.id">
                                            </td>
                                            <td><label :for="c.id">{{c.name}}</label></td>
                                        </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </el-tab-pane>
                        </el-tabs>
                    </template>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

            <!-- 编辑标签弹层 -->
            <div class="add-form">
                <el-dialog title="编辑用户" :visible.sync="dialogFormVisible4Edit">
                    <template>
                        <el-tabs v-model="activeName" type="card">
                            <el-tab-pane label="基本信息" name="first">
                                <el-form label-position="right" label-width="70px">
                                    <el-row>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="姓名">
                                                <el-input v-model="formData.username"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="出生日期">
                                                <el-input placeholder="YYYY-MM-DD" v-model="formData.date"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="性别">
                                                <el-select v-model="formData.gender">
                                                    <el-option label="男" value="男"></el-option>
                                                    <el-option label="女" value="女"></el-option>
                                                </el-select>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="联系方式">
                                                <el-input v-model="formData.telephone"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                </el-form>
                            </el-tab-pane>
                            <el-tab-pane label="角色信息" name="second">
                                <div class="checkScrol">
                                    <table class="datatable">
                                        <thead>
                                        <tr>
                                            <th>选择</th>
                                            <th>角色名</th>
                                        </tr>
                                        </thead>
                                        <tbody>
                                        <tr v-for="c in tableData">
                                            <td>
                                                <input :id="c.id" v-model="roleIds" type="checkbox" :value="c.id">
                                            </td>
                                            <td><label :for="c.id">{{c.name}}</label></td>
                                        </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </el-tab-pane>
                        </el-tabs>
                    </template>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                        <el-button type="primary" @click="handleEdit()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

            <!-- 修改密码标签弹层 -->
            <!--<div class="add-form">
                <el-dialog title="修改用户密码" :visible.sync="dialogFormVisible4EditPassword">
                    <template>
                        <el-tabs v-model="activeName" type="card">
                            <el-tab-pane label="修改密码" name="first">
                                <el-form label-position="right" label-width="70px">
                                    <el-row>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="请输入新密码">
                                                <el-input type="password" v-model="formData.firstNewPassword"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-form-item label="请再次输入新密码">
                                            <el-input type="password" v-model="formData.secondNewPassword"/>
                                        </el-form-item>
                                    </el-row>
                                </el-form>
                            </el-tab-pane>
                        </el-tabs>
                    </template>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible4EditPassword = false">取消</el-button>
                        <el-button type="primary" @click="handleEditPassword()">确定</el-button>
                    </div>
                </el-dialog>
            </div>-->


        </div>
    </div>
</div>
</body>

<script>
    var vue = new Vue({
        el: '#app',
        data: {
            activeName: 'first',//添加/编辑窗口Tab标签名称
            pagination: {//分页相关属性
                currentPage: 1,
                pageSize: 10,
                total: 100,
                queryString: null,
            },
            dataList: [],//列表数据
            formData: {},//表单数据
            tableData: [],//新增和编辑表单中对应的角色列表数据
            roleIds: [],//新增和编辑表单中角色对应的复选框，基于双向绑定可以进行回显和数据提交
            dialogFormVisible: false,//控制添加窗口显示/隐藏
            dialogFormVisible4Edit: false,//控制编辑窗口显示/隐藏
            dialogFormVisible4EditPassword: false//修改密码窗口显示/隐藏
        },
        created() {
            this.findPage();
        },
        methods: {
            //修改密码
            /*handleEditPassword(row) {
                console.log("row=======",row)

                axios.post("/user/updatePassword.do?id=" + row, this.formData).then(res => {
                    this.$message({
                        message: res.data.message,
                        type: res.data.flag ? "success" : "error"
                    });
                    if (res.data.flag) {
                        // 关闭窗口
                        this.dialogFormVisible4EditPassword = false;
                        // 刷新列表数据
                        this.findPage();
                    }
                })
            },*/
            //编辑
            handleEdit() {
                axios.post('/user/update.do?roleIds=' + this.roleIds, this.formData).then(res => {
                    this.$message({
                        message: res.data.message,
                        type: res.data.flag ? "success" : "error"
                    });
                    if (res.data.flag) {
                        // 关闭编辑窗口
                        this.dialogFormVisible4Edit = false;
                        // 刷新列表数据
                        this.findPage();
                    }
                })
            },
            //添加
            handleAdd() {
                // checkitemIds 通过url中key value提交，后台用@RequestParam 可以省略这个注解
                // this.formData, 是以payload, 请求体来提交，后台用@RequestBody 来接收
                axios.post('/user/add.do?roleIds=' + this.roleIds, this.formData).then(res => {
                    this.$message({
                        message: res.data.message,
                        type: res.data.flag ? "success" : "error"
                    });
                    if (res.data.flag) {
                        // 关闭新增窗口
                        this.dialogFormVisible = false;
                        // 刷新列表数据
                        this.findPage();
                    }
                })
            },
            //分页查询
            findPage() {
                axios.post('/user/findPage.do', this.pagination).then(res => {
                    if (res.data.flag) {
                        // 数据绑定
                        this.pagination.total = res.data.data.total;
                        // 分页结果集
                        this.dataList = res.data.data.rows;
                    } else {
                        this.$message.error(res.data.message);
                    }
                })
            },
            // 重置表单
            resetForm() {
                this.formData = {};
                this.tableData = [];
                this.roleIds = [];
                this.activeName = 'first';
            },
            // 弹出添加窗口
            handleCreate() {
                this.resetForm();
                this.dialogFormVisible = true;
                axios.get('/role/findAll.do').then(res => {
                    if (res.data.flag) {
                        // 角色列表
                        this.tableData = res.data.data;
                    } else {
                        this.$message.error(res.data.message);
                    }
                })
            },
            /*//弹出修改密码窗口
            handleUpdatePassword(row) {
                console.log("===========>>>>>",row.id)
                this.resetForm();
                this.dialogFormVisible4EditPassword = true;
                this.handleEditPassword(row.id);
            },*/
            // 弹出编辑窗口
            handleUpdate(row) {
                this.resetForm();
                this.dialogFormVisible4Edit = true;
                var id = row.id;// 用户ID
                axios.get('/user/findById.do?id=' + id).then(res => {
                    if (res.data.flag) {
                        // 用户信息回显
                        this.formData = res.data.data;
                        // 查询角色列表
                        axios.get('/role/findAll.do').then(res1 => {
                            if (res1.data.flag) {
                                // 角色列表
                                this.tableData = res1.data.data;
                                // 钩选角色
                                axios.get('/user/findRoleIdsByUserId.do?id=' + id).then(res2 => {
                                    if (res2.data.flag) {
                                        // 钩选
                                        this.roleIds = res2.data.data;
                                    } else {
                                        this.$message.error(res2.data.message);
                                    }
                                })
                            } else {
                                this.$message.error(res1.data.message);
                            }
                        })
                    } else {
                        this.$message.error(res.data.message);
                    }
                })
            },
            //切换页码
            handleCurrentChange(currentPage) {
                // 修改页码
                this.pagination.currentPage = currentPage;
                this.findPage();
            },
            // 删除
            handleDelete(row) {
                var id = row.id; // 用户id
                this.$confirm('此操作将【永久删除】该用户, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    // 点击确定时调用
                    axios.post('/user/deleteById.do?id=' + id).then(res => {
                        this.$message({
                            message: res.data.message,
                            type: res.data.flag ? "success" : "error"
                        });
                        if (res.data.flag) {
                            // 刷新列表
                            this.findPage();
                        }
                    })
                }).catch(() => {

                });
            },
            // 重置密码
            handleResetPassword(row) {
                var id = row.id; // 用户id
                this.$confirm('此操作将重置用户密码, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    // 点击确定时调用
                    axios.post('/user/resetById.do?id=' + id).then(res => {
                        this.$message({
                            message: res.data.message,
                            type: res.data.flag ? "success" : "error"
                        });
                        if (res.data.flag) {
                            // 刷新列表
                            this.findPage();
                        }
                    })
                }).catch(() => {

                });
            }
        }
    })
</script>
</html>
